package com.zhaoxi.springboot.restful.springbootrestful.controller;


import com.zhaoxi.springboot.restful.springbootrestful.entity.User;
import com.zhaoxi.springboot.restful.springbootrestful.result.Result;
import com.zhaoxi.springboot.restful.springbootrestful.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 欢迎来到牧码人教育，做Java我们是专业的
 *
 * @创建人： 牧码人教育-Gerry
 * @创建时间： 2020-4-26
 * @功能描述：  DELETE,PUT,POST,GET
 */
@RestController
@RequestMapping("v1/api")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("users")
    // @RequestMapping(method = RequestMethod.POST)
    public Result<User> saveUser(@RequestBody User user) {
        userService.save(user);
        return Result.SUCCESS(user);
    }

    @PutMapping("users")
    public Result<User> updateUser(@RequestBody User user) {
        userService.update(user);
        return Result.SUCCESS(user);
    }

    @DeleteMapping("users/{id}")
    public Result<String> deleteUser(@PathVariable("id") Integer id) {
        userService.remove(id);
        return Result.SUCCESS("删除成功");
    }

    @GetMapping("users/{id}")
    public Result<User> queryUserById(@PathVariable("id") Integer id) {
        User user = userService.queryUserById(id);
        return Result.SUCCESS(user);
    }

    @GetMapping("users")
    public Result<List<User>> queryAllUser() {
        List<User> users = userService.queryAll();
        return Result.SUCCESS(users);
    }

}
